﻿using System;
using System.Diagnostics;
using DevExpress.XtraEditors;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using eFatura.ceMessenger;

namespace eFatura.Utility
{
    public class LogManager
    {
        public static void Notify(Exception ex)
        {
            XtraMessageBox.Show(ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static int TakeScreenShot()
        {
            MemoryStream memoryStream = new System.IO.MemoryStream();
            Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            byte[] imageBytes = null;
            using (MemoryStream ms = new MemoryStream())
            {
                bmpScreenshot.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                imageBytes = ms.ToArray();
            }
            object result = AppSession.DataManager.ExecuteScalar("InsScreenShoot", AppSession.UserInfo.UserId, imageBytes);
            return (int)result;
        }

        public static void LogToEventLog(UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            try
            {
                // Since we can't prevent the app from terminating, log this to the event log.
                if (!EventLog.SourceExists("ThreadException"))
                    EventLog.CreateEventSource("ThreadException", "Application");

                // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source = "ThreadException";
                myLog.WriteEntry("ConfExpert'te hata oluştu. Hata Bilgisi :\n\n" + ex.ToString() + "\n\nStack Trace:\n" + ex.StackTrace);
            }
            catch
            {
            //    XtraMessageBox.Show("Hata kayıt altına alınamadı! Lütfen IT ekibine haber veriniz. Hata: " + ex.ToString(), "Sistem Hatası");
            }
        }

        public static void LogToDB(string str)
        {
            try
            {
                //if (!Ex.Contains("0x80131904"))
                AppSession.DataManager.ExecuteScalar("COCatch", str, AppSession.Version, /*File.ReadAllBytes(FilePath),*/ AppSession.UserInfo.UserId);
                XtraMessageBox.Show(str, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ceMessengerSoapClient messenger = new ceMessengerSoapClient();
                
                //TODO: messenger şifresi taşınmamalı !!!
                messenger.SendEmail("ce", "---", "eFatura", "oaykut@gmail.com", "EFatura Exception", str, "kasimgulcan.karbel@gmail.com,kadir.akis@karbel.com,burak.sahin@karbel.com");
            }
            catch
            {
                XtraMessageBox.Show("Exception could not be logged. Please contact to IT team. Exception: " + str, "System Error");
            }
        }

        public static void LogToDB(Exception ex)
        {
            LogToDB(ex.ToString());
        }
    }
}